import wx from "jweixin-module"
import globalData from '@/config'
/**
 * @Author: liuxu
 * @description: 通过config接口注入权限验证配置
 * @param {Array | String}  jsApiList   使用的接口列表
 * @return {*}
 */
wx.getJsConfig = (jsApiList = [
	"updateAppMessageShareData",
	"updateTimelineShareData",
	"getLocation"
]) => {
	return new Promise((resolve, reject) => {
		let url = encodeURIComponent(window.location.href.split('#')[0])
		uni.request({
			url: `${globalData.http_host}/user/get_wechat_config?url=${url}`,
			method: "GET",
			data: {},
			headers: {
				"Content-Type": "application/x-www-form-urlencoded",
			},
			success: (res) => {
				try {
					if (res.data.code == 200001) {
						let data = res.data.data
						let appId = data.appId
						let timestamp = data.timestamp
						let nonceStr = data.nonceStr
						let signature = data.signature
						wx.config({
							debug: false, //是否打开调试
							appId: appId, // 公众号的唯一标识
							timestamp: timestamp, // 生成签名的时间戳
							nonceStr: nonceStr, // 生成签名的随机串
							signature: signature, // 签名
							jsApiList: typeof jsApiList == 'string'?[jsApiList]:jsApiList, // 这里传一个和多个判断下
						})
						// config信息验证成功会执行ready函数
						wx.ready(res => {
							console.log('config信息验证成功')
							resolve(true);
						})
						wx.error(error => {
							console.log('config信息验证失败',error)
							reject(error);
						})
					}
				} catch (error) {
					console.warn("通过config接口注入权限验证配置失败,请检查是否在正式环境", error)
					reject(error);
				}
			}
		});
	})
}
export default wx
